<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="获奖时间" prop="winningTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.winningTime"
          type="year"
          value-format="yyyy-MM-dd"
          placeholder="选择获奖时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="专业" prop="deptName">
        <el-select v-model="queryParams.deptName" placeholder="请选择专业" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_dept_name"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学历" prop="studentEdu">
        <el-select v-model="queryParams.studentEdu" placeholder="请选择学历" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_student_edu"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="年级" prop="studentGrade">
        <el-select v-model="queryParams.studentGrade" placeholder="请选择年级" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_student_grade"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="竞赛名称" prop="gameName">
        <el-input
          v-model="queryParams.gameName"
          placeholder="请输入竞赛名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="奖项等级" prop="level">
        <el-select v-model="queryParams.level" placeholder="请选择奖项等级" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_award_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:award:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:award:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:award:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:award:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="awardList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="获奖ID" align="center" prop="awardId" />
      <el-table-column label="获奖时间" align="center" prop="winningTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.winningTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学号" align="center" prop="studentNumber" />
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="专业" align="center" prop="deptName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_dept_name" :value="scope.row.deptName"/>
        </template>
      </el-table-column>
      <el-table-column label="学历" align="center" prop="studentEdu">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_student_edu" :value="scope.row.studentEdu"/>
        </template>
      </el-table-column>
      <el-table-column label="年级" align="center" prop="studentGrade">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_student_grade" :value="scope.row.studentGrade"/>
        </template>
      </el-table-column>
      <el-table-column label="班级" align="center" prop="studentClass">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_student_class" :value="scope.row.studentClass"/>
        </template>
      </el-table-column>
      <el-table-column label="电话" align="center" prop="studentTel" />
      <el-table-column label="竞赛名称" align="center" prop="gameName" />
      <el-table-column label="组织机构" align="center" prop="gameOrganization" />
      <el-table-column label="参赛项目" align="center" prop="gameSubject" />
      <el-table-column label="竞赛类型" align="center" prop="gameType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_game_type" :value="scope.row.gameType"/>
        </template>
      </el-table-column>
      <el-table-column label="团队名称" align="center" prop="teamName" />
      <el-table-column label="奖项等级" align="center" prop="level">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_award_level" :value="scope.row.level"/>
        </template>
      </el-table-column>
      <el-table-column label="获奖批文号/网址" align="center" prop="approvalNumber" />
      <el-table-column label="证书编号" align="center" prop="certificateNumber" />
      <el-table-column label="降级竞赛" align="center" prop="domotion">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.domotion"/>
        </template>
      </el-table-column>
      <el-table-column label="第一指导教师" align="center" prop="don1" />
      <el-table-column label="第二指导教师" align="center" prop="don2" />
      <el-table-column label="第三指导教师" align="center" prop="don3" />
      <el-table-column label="涉航空类" align="center" prop="aviation">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.aviation"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:award:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:award:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改获奖信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="获奖时间" prop="winningTime">
              <el-date-picker style="width: 173.33px  "
                              v-model="form.winningTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择获奖时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学生学号" prop="studentNumber">
              <el-input v-model="form.studentNumber" placeholder="请输入学号" @blur="autoCompleteStudent($event)"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学生姓名" prop="studentName">
              <el-input v-model="form.studentName" placeholder="请输入学生姓名" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="学生性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学生专业" prop="deptName">
              <el-select v-model="form.deptName" placeholder="请选择专业">
                <el-option
                  v-for="dict in dict.type.sys_dept_name"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学生学历" prop="studentEdu">
              <el-select v-model="form.studentEdu" placeholder="请选择学历">
                <el-option
                  v-for="dict in dict.type.sys_student_edu"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="学生年级" prop="studentGrade">
              <el-select v-model="form.studentGrade" placeholder="请选择年级">
                <el-option
                  v-for="dict in dict.type.sys_student_grade"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学生班级" prop="studentClass">
              <el-select v-model="form.studentClass" placeholder="请选择班级">
                <el-option
                  v-for="dict in dict.type.sys_student_class"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学生电话" prop="studentTel">
              <el-input v-model="form.studentTel" placeholder="请输入电话" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="竞赛名称" prop="gameName">
              <el-input v-model="form.gameName" placeholder="请输入竞赛名称"  @blur="autoCompleteGame($event)"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="组织机构" prop="gameOrganization">
              <el-input v-model="form.gameOrganization" placeholder="请输入组织机构" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="竞赛类型" prop="gameType">
              <el-select v-model="form.gameType" placeholder="请选择竞赛类型">
                <el-option
                  v-for="dict in dict.type.sys_game_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="参赛项目" prop="gameSubject">
              <el-input v-model="form.gameSubject" placeholder="请输入参赛项目" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="团队名称" prop="teamName">
              <el-input v-model="form.teamName" placeholder="请输入团队名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="奖项等级" prop="level">
              <el-select v-model="form.level" placeholder="请选择奖项等级">
                <el-option
                  v-for="dict in dict.type.sys_award_level"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="获奖网址" prop="approvalNumber">
              <el-input v-model="form.approvalNumber" placeholder="请输入获奖批文号/网址" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="证书编号" prop="certificateNumber">
              <el-input v-model="form.certificateNumber" placeholder="请输入证书编号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="降级竞赛">
              <el-radio-group v-model="form.domotion">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="第一导师" prop="don1">
              <el-input v-model="form.don1" placeholder="请输入第一指导教师" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="第二导师" prop="don2">
              <el-input v-model="form.don2" placeholder="请输入第二指导教师" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="第三导师" prop="don3">
              <el-input v-model="form.don3" placeholder="请输入第三指导教师" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="涉航空类">
          <el-radio-group v-model="form.aviation">
            <el-radio
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAward, getAward, delAward, addAward, updateAward } from "@/api/system/award";
import { getStudentNumber } from "@/api/system/student";
import { getGamesName } from "../../../api/system/games";

export default {
  name: "Award",
  dicts: ['sys_student_edu', 'sys_student_class', 'sys_yes_no', 'sys_game_type', 'sys_user_sex', 'sys_dept_name', 'sys_student_grade', 'sys_award_level'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 获奖信息表格数据
      awardList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        winningTime: null,
        deptName: null,
        studentEdu: null,
        studentGrade: null,
        gameName: null,
        level: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        winningTime: [
          { required: true, message: "获奖时间不能为空", trigger: "blur" }
        ],
        studentNumber: [
          { required: true, message: "学号不能为空", trigger: "blur" }
        ],
        studentName: [
          { required: true, message: "学生姓名不能为空", trigger: "blur" }
        ],
        deptName: [
          { required: true, message: "专业不能为空", trigger: "change" }
        ],
        studentEdu: [
          { required: true, message: "学历不能为空", trigger: "change" }
        ],
        studentGrade: [
          { required: true, message: "年级不能为空", trigger: "change" }
        ],
        studentClass: [
          { required: true, message: "班级不能为空", trigger: "change" }
        ],
        gameName: [
          { required: true, message: "竞赛名称不能为空", trigger: "blur" }
        ],
        gameSubject: [
          { required: true, message: "参赛项目不能为空", trigger: "blur" }
        ],
        gameType: [
          { required: true, message: "竞赛类型不能为空", trigger: "change" }
        ],
        level: [
          { required: true, message: "奖项等级不能为空", trigger: "change" }
        ],
        domotion: [
          { required: true, message: "降级竞赛不能为空", trigger: "blur" }
        ],
        don1: [
          { required: true, message: "第一指导教师不能为空", trigger: "blur" }
        ],
        aviation: [
          { required: true, message: "涉航空类不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /**表单根据所输入的学号自动补全学生信息**/
    autoCompleteStudent(){
      getStudentNumber(this.form.studentNumber).then(response => {
        if(response.data != null){
          this.form.studentName = response.data.studentName;
          this.form.sex = response.data.sex;
          this.form.deptName = response.data.deptName;
          this.form.studentEdu = response.data.studentEdu;
          this.form.studentGrade = response.data.studentGrade;
          this.form.studentClass = response.data.studentClass;
          this.form.studentTel = response.data.studentTel;
        }
      });
    },

    /**表单根据所输入的竞赛名称自动补全竞赛信息**/
    autoCompleteGame(){
      getGamesName(this.form.gameName).then(response => {
        if(response.data != null){
          this.form.gameType = response.data.gameType;
          this.form.gameOrganization = response.data.gameOrganization;
          this.form.domotion = response.data.domotion;
          this.form.aviation = response.data.aviation;
        }
      });
    },

    /** 查询获奖信息列表 */
    getList() {
      this.loading = true;
      listAward(this.queryParams).then(response => {
        this.awardList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        awardId: null,
        winningTime: null,
        studentNumber: null,
        studentName: null,
        sex: null,
        deptName: null,
        studentEdu: null,
        studentGrade: null,
        studentClass: null,
        studentTel: null,
        gameName: null,
        gameOrganization: null,
        gameSubject: null,
        gameType: null,
        teamName: null,
        level: null,
        approvalNumber: null,
        certificateNumber: null,
        domotion: "0",
        don1: null,
        don2: null,
        don3: null,
        aviation: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.awardId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加获奖信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const awardId = row.awardId || this.ids
      getAward(awardId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改获奖信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.awardId != null) {
            updateAward(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAward(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const awardIds = row.awardId || this.ids;
      this.$modal.confirm('是否确认删除获奖信息编号为"' + awardIds + '"的数据项？').then(function() {
        return delAward(awardIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/award/export', {
        ...this.queryParams
      }, `award_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
